package com.tenbit.emp.svc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import com.tenbit.emp.dao.CodeZipEDAO;
import com.tenbit.emp.dao.EmployeeEDAO;
import com.tenbit.emp.dao.EmployeeQEMP1000DAO;
import com.tenbit.emp.dao.EmployeeSkillEDAO;
import com.tenbit.emp.vo.EMP1000VO;
import com.tenbit.emp.vo.EMP1010VO;

import emp.tenbit.emp.bean.PagingBean;
import emp.tenbit.emp.bean.SearchEmpBean;

@Component
public class EMP1000SVC {
	@Autowired
	private EmployeeEDAO empEdao;
	@Autowired
	private CodeZipEDAO zipEDAO;
	@Autowired
	private EmployeeQEMP1000DAO qemp1000dao;
	@Autowired
	private EmployeeSkillEDAO skillEDAO;
	
	private Logger log=Logger.getLogger(this.getClass());
	
	public List<Map<String, Object>> getDeptNamelist(Map<String, Object> paraMap){
		return qemp1000dao.listEMP(paraMap);
	}
	
	public List<EMP1010VO> getEmpSearchList(String name){
		List<EMP1010VO> list=new ArrayList<EMP1010VO>();
		SearchEmpBean bean=new SearchEmpBean();
		
		bean.setName(name);	
		list=qemp1000dao.listEmployeeJoin(bean);
		
		return list;
	}
	public ModelAndView getEmpList(SearchEmpBean emp,String page,ModelAndView mv){
		   int pageScale=10;
		   int currentPage=0;
		   int blockScale=10;
		   try{
		   int totalRow=qemp1000dao.selectCountEmployee(emp);
		   int totalPage=totalRow%pageScale==0?totalRow/pageScale:(totalRow/pageScale)+1;
		   if(totalRow==0)totalPage=1;
		   String tempPage="";
		   tempPage=page;
		   if(tempPage==null)tempPage="1";
			   currentPage=Integer.parseInt(tempPage);
			   int start=1+(currentPage-1)*pageScale;
			   int end=pageScale+(currentPage-1)*pageScale;
			   emp.setStart(start);
			   emp.setEnd(end);
			   mv.addObject("listEmp", qemp1000dao.listEmployeeJoin(emp));
			   int currentBlock=currentPage%blockScale==0?currentPage/blockScale:currentPage/blockScale+1;
			   int startPage=1+(currentBlock-1)*blockScale;
			   int endPage=blockScale+(currentBlock-1)*blockScale;
			   if(totalPage<=endPage){
				   endPage=totalPage;
			   }
			   mv.addObject("pageBean",new PagingBean(startPage, endPage, currentPage,currentBlock, totalPage) );
			   mv.addObject("searchBean",emp);
			   log.debug("PagingBean "+startPage+" "+endPage+" "+currentBlock+" "+currentPage+" "
			   +totalPage+" "+start+" "+end);
			   log.debug("searchBean"+emp);
		   }catch(Exception e){
			   currentPage=1;
		   }
		
		   return mv;
	}
	public List<EMP1000VO> getCodeZipByMvList(){
		return zipEDAO.listCodeZipByMv();
	}
	public void getDelOrModList(ModelAndView mv,String empId){
		EMP1010VO emp=empEdao.selectEmployee(empId);
		List<String> skillList=skillEDAO.listEmpSkill(empId);
		Map<String, Object> map=new HashMap<String, Object>();
		
		map.put("name", emp.getName());
		map.put("person_id1", emp.getPerson_id().substring(0, 6));
		map.put("person_id2", emp.getPerson_id().substring(7));
		map.put("dept_code", emp.getDept_code());
		map.put("academic_code",emp.getAcademic_code());
		map.put("sk",skillList);
		map.put("grad_stime_year", emp.getGrad_sdate().substring(0,4));
		map.put("grad_stime_month", emp.getGrad_sdate().substring(5,7));
		map.put("grad_stime_day", emp.getGrad_sdate().substring(8));
		map.put("grad_etime_year", emp.getGrad_edate().substring(0,4));
		map.put("grad_etime_month", emp.getGrad_edate().substring(5,7));
		map.put("grad_etime_day", emp.getGrad_edate().substring(8));
		map.put("salary", emp.getSalary());
		
		log.debug("getDelOrModList"+map);
		mv.addObject("viewBean", map);
	}
	public Map<String, Object> doMakeView(SearchEmpBean emp){
		Map<String, Object> map=new HashMap<String, Object>();
		log.debug("doMakeView"+emp);
		
		map.put("name", emp.getName());
		
		if(emp.getSex_code() != null){
			map.put("sx", emp.getSex_code());
			
		}
		
		map.put("dept_code", emp.getDept_code());
			
		
		if(emp.getAcademic_code() != null){
			map.put("aca",emp.getAcademic_code());
			
		}
		if(emp.getSkill_code() != null){
			map.put("sk",emp.getSkill_code());
			
		}
		if(!emp.getGrad_sdate().equals("")){
			map.put("grad_stime_year", emp.getGrad_sdate().substring(0,4));
			map.put("grad_stime_month", emp.getGrad_sdate().substring(4,6));
			map.put("grad_stime_day", emp.getGrad_sdate().substring(6));
			
		}
		if(!emp.getGrad_edate().equals("")){
			map.put("grad_etime_year", emp.getGrad_edate().substring(0,4));
			map.put("grad_etime_month", emp.getGrad_edate().substring(4,6));
			map.put("grad_etime_day", emp.getGrad_edate().substring(6));
			
		}
		
		return map;
		
	}
}
